package com.authorization.framework.task.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.authorization.client.client.util.secret.HttpUtil;
import com.authorization.common.utils.DateUtils;
import com.authorization.common.utils.SecurityUtils;
import com.authorization.framework.task.RyTaskService;
import com.authorization.framework.task.domian.RepoInfo;
import com.authorization.project.authorization.domain.Microservice;
import com.authorization.project.authorization.service.IMicroserviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @Author 李晓雷
 * @Date 2020/6/23 10:26
 * @Version 1.0
 */
@Service
@Slf4j
public class RyTaskServiceImpl implements RyTaskService {

    @Value("${smartOne.service.url}")
    private String url;

    @Autowired
    private IMicroserviceService iMicroserviceService;

    /**
     * smartOne微服务同步
     */
    @Override
    @Transactional
    public void smartOneServiceSynchro(long productId) {
        /**1 http调用repo接口获取微服务列表**/
        try {
            String  restStr = HttpUtil.doPost(HttpUtil.getPost(url, new HashMap<>()));
            JSONObject jsonObject = JSONObject.parseObject(restStr);
            int total = (Integer)jsonObject.get("total");
            if(total == 0 ){
                if(log.isDebugEnabled()){
                    log.debug("没有微服务数据");
                }
                return;
            }
            /**2 遍历集合取 repoType2 后端 **/
            /**2.1  delFlag 状态 = 0 正常 更新数据库**/
            /**2.2  delFlag 状态 = 2 删除数据**/
            List<RepoInfo> repoInfoList = JSON.parseArray(jsonObject.getString("rows"), RepoInfo.class);

            List<Microservice> microserviceList = new ArrayList<>();

            repoInfoList.stream().forEach(repoInfo -> {
                if(2 != repoInfo.getRepoType()){
                    return;
                }
                if(Objects.equals("0", repoInfo.getDelFlag())){
                    Microservice microservice = new Microservice();
                    int num = iMicroserviceService.selectNumByServicenameAndProductId(repoInfo.getServiceName(), productId);
                    if(num == 0){
                        microservice.setServicename(repoInfo.getServiceName());
                        microservice.setServicenamecn(repoInfo.getRemark());
                        microservice.setProductId(productId);
                        microservice.setBaseState("0");
                        microservice.setCreateBy("admin");
                        microservice.setCreateTime(DateUtils.getNowDate());
                        microserviceList.add(microservice);
                    }
                }

                if(Objects.equals("2", repoInfo.getDelFlag())){
                    iMicroserviceService.deleteMicroserviceByServicenameAndProductId(repoInfo.getServiceName(), productId);
                    log.info("删除微服务{}", repoInfo.getServiceName());
                }
            } );

            if(microserviceList.size() > 0){
                iMicroserviceService.batchInsert(microserviceList);
                log.info("新增数量{}", microserviceList.size());
            }
        } catch (UnsupportedEncodingException e) {
            log.info("获取smartOne微服务数据失败{}", e.getMessage());
        }




    }
}
